/**
 * 组件安装
 * npm install gulp jshint -g
 * npm install gulp-util gulp-imagemin gulp-less gulp-clean-css gulp-jshint gulp-uglify gulp-rename gulp-concat gulp-clean gulp-sourcemaps gulp-pug browser-sync yargs --save-dev
 */
// 引入 gulp及组件
var gulp    = require('gulp'),                 //基础库
    imagemin = require('gulp-imagemin'),       //图片压缩
    less = require('gulp-less'),               //less
    minifycss = require('gulp-clean-css'),     //css压缩
    jshint = require('gulp-jshint'),           //js检查
    uglify  = require('gulp-uglify'),          //js压缩
    rename = require('gulp-rename'),           //重命名
    concat  = require('gulp-concat'),          //合并文件
    clean = require('gulp-clean'),             //清空文件夹
    sourcemaps = require('gulp-sourcemaps'),    //maps
    browserSync = require('browser-sync'),      //浏览器
    pug=require('gulp-pug'),                    //引擎
    yargs= require('yargs').argv;               //启动后缀

// HTML处理
gulp.task('html', function() {
    var htmlSrc = './src/*.html',
        htmlDst = './dist/';
    gulp.src(htmlSrc)
        .pipe(browserSync.reload({stream:true}))
        .pipe(gulp.dest(htmlDst))
});

// 样式处理
gulp.task('css', function () {
    var cssSrc = './src/less/*.less',
        cssDst = './dist/css';
    gulp.src(cssSrc)
        .pipe(sourcemaps.init())
        .pipe(less())
        .pipe(gulp.dest(cssDst))
        .pipe(browserSync.reload({stream:true}))
        .pipe(rename({ suffix: '.min' }))
        .pipe(minifycss())
        .pipe(gulp.dest(cssDst));
});

// 图片处理
gulp.task('images', function(){
    var imgSrc = './src/images/**/*',
        imgDst = './dist/images';
    gulp.src(imgSrc)
        .pipe(imagemin())
        .pipe(browserSync.reload({stream:true}))
        .pipe(gulp.dest(imgDst));
})

// js处理
gulp.task('js', function () {
    var jsSrc = './src/js/*.js',
        jsDst ='./dist/js';
    gulp.src(jsSrc)
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(concat('main.js'))
        .pipe(gulp.dest(jsDst))
        .pipe(browserSync.reload({stream:true}))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest(jsDst));
});

//pug处理
gulp.task('pug',function () {
    gulp.src("./src/*.pug")
    .pipe(pug({pretty: true}))
    .pipe(browserSync.reload({stream:true}))
    .pipe(gulp.dest("./dist/"));
});

// 清空图片、样式、js
gulp.task('clean', function() {
    gulp.src(['./dist/css', './dist/js', './dist/images'], {read: false})
        .pipe(clean());
});

gulp.task('release', ['html', 'js','css','images','pug']);

// 监听任务 运行语句 gulp watch
gulp.task('watch',['release'],function(){
        // 监听html
        gulp.watch('./src/*.html', function(event){
            gulp.run('html');
        })
        // 监听css
        gulp.watch('./src/scss/*.scss', function(){
            gulp.run('css');
        });
        // 监听images
        gulp.watch('./src/images/**/*', function(){
            gulp.run('images');
        });
        // 监听js
        gulp.watch('./src/js/*.js', function(){
            gulp.run('js');
        });
        // 监听pug
        gulp.watch('./src/*.pug', function(){
            gulp.run('pug');
        });
});

gulp.task('server', function () {
    yargs.p = yargs.p || 8080;
    browserSync.init({
        server: {
            baseDir: "./dist"
        },
        ui: {
            port: yargs.p + 1,
            weinre: {
                port: yargs.p + 2
            }
        },
        port: yargs.p,
    });
});

// 参数说明
//  -w: 实时监听
//  -s: 启动服务器
//  -p: 服务器启动端口，默认8080
gulp.task('default', ['release'], function () {
    if (yargs.s) {
        gulp.start('server');
    }

    if (yargs.w) {
        gulp.start('watch');
    }
});